package com.codedef.diaryserver.controller;

import com.codedef.diaryserver.domain.vo.UserLoginVo;
import com.codedef.diaryserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public String userLogin(@RequestParam String username, @RequestParam String password, HttpSession session) {
        if (session.getAttribute("USER_NAME") != null && !session.getAttribute("USER_NAME").equals("")) {
            return "无须再次登录";
        } else if (userService.userLogin(username, password)) { // 查数据库
            session.setAttribute("USER_NAME", username);
            session.setAttribute("USER_ID", userService.getUserId(username));
            return "登录成功";
        }
        return "失败";
    }


}
